package com.library.controller;

import java.util.concurrent.atomic.AtomicInteger;

import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

@Controller
public class BaseController {
 
	private static final AtomicInteger counter = new AtomicInteger(0);
	private static final String VIEW_INDEX = "index";
	private final static org.slf4j.Logger logger = LoggerFactory.getLogger(BaseController.class);
	
	@RequestMapping(value = "/", method = RequestMethod.GET)
	public String welcome(ModelMap model) {

		model.addAttribute("message", "Welcome");
		model.addAttribute("counter", counter.incrementAndGet());
		logger.debug("[welcome] counter : {}", counter);
 
		// Spring uses InternalResourceViewResolver and return back index.jsp
		return VIEW_INDEX;

	}
 
	@RequestMapping(value = "/{name}", method = RequestMethod.GET)
	public String welcomeName(@PathVariable String name, ModelMap model) {
 
		model.addAttribute("message", "Welcome " + name);
		model.addAttribute("counter", counter.incrementAndGet());
		logger.debug("[welcomeName] counter : {}", counter.get());
		return VIEW_INDEX;
 
	}
 
}
